package com.xdog.okx.controller;

import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.okx.domain.log.*;
import com.xdog.okx.service.IOkxLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * OKX 日志数据控制器
 */
@Tag(name = "OKX 日志数据", description = "OKX 日志数据控制器")
@RestController
@RequestMapping("/okx/log")
public class OkxLogController {

    @Resource
    private IOkxLogService okxLogService;

    /**
     * 查询指定区块和地址事件日志
     */
    @Operation(summary = "查询指定区块和地址事件日志", description = "根据链短名称、区块高度范围和地址查询事件日志")
    @GetMapping("/by-block-and-address")
    public R<JSONObject> getLogByBlockAndAddress(LogByBlockAndAddressQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("startBlockHeight", query.getStartBlockHeight());
            params.put("endBlockHeight", query.getEndBlockHeight());
            params.put("address", query.getAddress());

            JSONObject logData = okxLogService.getLogByBlockAndAddress(params);
            return R.ok(logData);
        } catch (Exception e) {
            return R.fail("查询指定区块和地址事件日志失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定地址和 topic 事件日志
     */
    @Operation(summary = "查询指定地址和 topic 事件日志", description = "根据链短名称、地址和 topic 查询事件日志")
    @GetMapping("/by-address-and-topic")
    public R<JSONObject> getLogByAddressAndTopic(LogByAddressAndTopicQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            params.put("topic0", query.getTopic0());

            JSONObject logData = okxLogService.getLogByAddressAndTopic(params);
            return R.ok(logData);
        } catch (Exception e) {
            return R.fail("查询指定地址和 topic 事件日志失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定地址事件日志
     */
    @Operation(summary = "查询指定地址事件日志", description = "根据链短名称和地址查询事件日志")
    @GetMapping("/by-address")
    public R<JSONObject> getLogByAddress(LogByAddressQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());

            JSONObject logData = okxLogService.getLogByAddress(params);
            return R.ok(logData);
        } catch (Exception e) {
            return R.fail("查询指定地址事件日志失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定哈希事件日志
     */
    @Operation(summary = "查询指定哈希事件日志", description = "根据链短名称和交易哈希查询事件日志")
    @GetMapping("/by-transaction")
    public R<JSONObject> getLogByTransaction(LogByTransactionQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());

            JSONObject logData = okxLogService.getLogByTransaction(params);
            return R.ok(logData);
        } catch (Exception e) {
            return R.fail("查询指定哈希事件日志失败: " + e.getMessage());
        }
    }
}